package kr.couponplay.android.domain.deal;

import java.util.ArrayList;
import java.util.List;

import kr.couponplay.android.support.JsonUtils;
import kr.couponplay.android.support.LinkUtils;
import kr.couponplay.android.support.StringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class DealJsonParser {
	public List<Deal> makeDealList(String sid, JSONArray jsonArray) throws JSONException {
		List<Deal> deals = new ArrayList<Deal>();
		
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject object = jsonArray.getJSONObject(i);

			Deal deal = new Deal();
			deal.setId(object.getInt("id"));
			deal.setCategoryId(object.getInt("categoryId"));
			deal.setTitle(object.getString("title"));
			deal.setMetaId(object.getString("metaId"));
			deal.setDiscountRate(object.getInt("discountRate"));
			deal.setNowCount(object.getInt("nowCount"));
			deal.setProviderCode(object.getString("providerCode"));
			deal.setCategoryName(StringUtils.trimToEmpty(object.getString("categoryName")));
			deal.setOriginalPrice(object.getInt("originalPrice"));
			deal.setSalesPrice(object.getInt("salesPrice"));
			deal.setFromDate(object.getString("saleStartAt"));
			deal.setToDate(object.getString("saleEndAt"));
			deal.setThumbnailImageUrl1(object.getString("imageUrl"));
			deal.setThumbnailImageUrl2(object.getString("imageUrl1"));
			deal.setShopName(object.getString("shopName"));
			deal.setShopLatitude(JsonUtils.getStringValue(object, "shopLatitude"));
			deal.setShopLongitude(JsonUtils.getStringValue(object, "shopLongitude"));
			deal.setDetailViewUrl(LinkUtils.landingPage(sid, deal.getId(), deal.getProviderCode(), deal.getMetaId()));
			deal.setDistance(JsonUtils.getStringValue(object, "distance"));
			deal.setViewDate(object.getString("viewDate"));
			deals.add(deal);
		}
		
		return deals;
	}
}
